#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    int rob(vector<int>& nuvms) {
        if (nuvms.size() == 0)
            return 0;
        if (nuvms.size() == 1)
            return nuvms[0];
        vector<int> dp(nuvms.size() + 1, 0);
        dp[0] = 0;
        dp[1] = nuvms[0];
        for (int i = 2; i <= nuvms.size(); i++)
        {
            dp[i] = max(dp[i - 2] + nuvms[i - 1], dp[i - 1]);
        }
        return dp[nuvms.size()];
    }
};

class Solution {
public:
    int Getnum(vector<int>& nums, int num0, int num1)
    {
        vector<int> dp(nums.size() - 1, 0);
        dp[0] = nums[num0];
        dp[1] = max(nums[num0], nums[num1]);
        for (int i = 2; i < nums.size() - 1; i++)
        {
            dp[i] = max(dp[i - 2] + nums[i + num0], dp[i - 1]);
        }
        return dp[nums.size() - 2];
    }

    int rob(vector<int>& nums) {
        if (nums.size() == 0)
            return 0;
        if (nums.size() == 1)
            return nums[0];
        if (nums.size() == 2)
            return max(nums[0], nums[1]);
        int dp1 = Getnum(nums, 0, 1);
        int dp2 = Getnum(nums, 1, 2);
        return max(dp1, dp2);
    }
}; 

class Solution {
public:
    vector<int> robTree(TreeNode* root)
    {
        if (root == nullptr)
            return vector<int>{0, 0};
        vector<int> left = robTree(root->left);
        vector<int> right = robTree(root->right);
        int val1 = root->val + left[0] + right[0];
        int val2 = max(left[0], left[1]) + max(right[0], right[1]);
        return vector<int>{val2, val1};
    }
    int rob(TreeNode* root) {
        vector<int> dp = robTree(root);
        return max(dp[0], dp[1]);
    }
};